import pymysql

# 创建链接
conn = pymysql.connect(host='localhost',port =3306,user='root',passwd='root',db='books',charset='utf8')

# 获取游标
cursor = conn.cursor()

#执行sql
# cursor.execute("select version()")
# result = cursor.fetchone()
# print(result)

# 查询图书表的数据
sql = "select id, title, 'read', 'comment' from t_book;"
cursor.execute(sql)

# 获取查询结果的总记录数
print("获取的查询结果记录行数为:",  cursor.rowcount)

# 获取查询结果的第一条数据
print(cursor.fetchone())

# 获取全部查询的结果
print(cursor.fetchall())

# 关闭游标
cursor.close()

# 关闭连接
conn.close()